﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Grocery
{
    sealed class CustomerFactory
    {
        private static int TryParseFromText(string text)
        {
            int i = 0;
            if (int.TryParse(text, out i) && i >= 0)
            {
                //TODO:
            }
            return i;
        }

        internal static Customer MakeCustomer(int number, string line)
        {
            Regex reg = new Regex(@"^(A|B)\s(\d)+\s(\d)+$");
            if (reg.IsMatch(line))
            {
                Match m = reg.Match(line);

                if (m.Groups.Count != 4)
                {
                    DebugInfo.Error("Invalid input: " + line);
                }
                
                CustomerType type = (CustomerType)Enum.Parse(typeof(CustomerType), m.Groups[1].Value);

                switch (type)
                {
                    case CustomerType.A:
                        return new CustomerA(
                            number,
                            TryParseFromText(m.Groups[2].Value), 
                            TryParseFromText(m.Groups[3].Value));
                    case CustomerType.B:
                        return new CustomerB(
                            number,
                            TryParseFromText(m.Groups[2].Value), 
                            TryParseFromText(m.Groups[3].Value));
                    default:
                        DebugInfo.Error("Invalid input: " + line);
                        return null;
                }
            }
            else
            {
                DebugInfo.Error("Invalid input: " + line);
                return null;
            }
        }
    }
}
